﻿namespace FR.SAFTAnalyserLite.ViewModels.Pane
{
    using FR.SAFTAnalyserLite.ViewModels.Base;
    using OfficeOpenXml;
    using OfficeOpenXml.Table;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;

    public class PaneViewModel : ViewModelBase
    {
        public PaneViewModel()
        {
            //Commands
            DoPrintCommand = new Cinch.SimpleCommand<object, object>(CanPrint, Print);
        }

        #region Title

        private string _title = null;
        public virtual string Title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    NotifyPropertyChanged(ObservableHelper.CreateArgs<PaneViewModel>(x => x.Title));
                }
            }
        }

        #endregion

        public virtual Uri IconSource { get; protected set; }

        #region ContentId

        private string _contentId = null;
        public string ContentId
        {
            get { return _contentId; }
            set
            {
                if (_contentId != value)
                {
                    _contentId = value;
                    NotifyPropertyChanged(ObservableHelper.CreateArgs<PaneViewModel>(x => x.ContentId));
                }
            }
        }
        #endregion

        #region IsSelected

        private bool _isSelected = false;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    NotifyPropertyChanged(ObservableHelper.CreateArgs<PaneViewModel>(x => x.IsSelected));
                }
            }
        }

        #endregion

        #region IsActive

        private bool _isActive = false;
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                if (_isActive != value)
                {
                    _isActive = value;
                    NotifyPropertyChanged(ObservableHelper.CreateArgs<PaneViewModel>(x => x.IsActive));
                }
            }
        }

        #endregion

        #region Print command
        public Cinch.SimpleCommand<object, object> DoPrintCommand { get; private set; }
        private void Print(object args)
        {
            DataGrid datagrid = args as DataGrid;
            if (datagrid != null)
            {
                if (datagrid.ItemsSource != null)
                {
                    DataTable dt = new DataTable();
                    foreach (DataGridColumn column in datagrid.Columns)
                    {
                        if (column is DataGridTextColumn && column.Visibility == Visibility.Visible)
                            dt.Columns.Add(column.Header.ToString());
                    }

                    foreach (DataGridRow row in GetDataGridRows(datagrid))
                    {
                        List<object> rows = new List<object>();
                        foreach (DataGridColumn column in datagrid.Columns)
                        {
                            if (column is DataGridTextColumn)
                            {
                                TextBlock txt = column.GetCellContent(row) as TextBlock;
                                if (txt != null)
                                    rows.Add(txt.Text);
                                else
                                    rows.Add("");
                            }
                        }
                        dt.Rows.Add(rows.ToArray());
                    }

                    ExcelPackage pck = new ExcelPackage();
                    var wsEnum = pck.Workbook.Worksheets.Add("SAFT");
                    //Load the collection starting from cell A1...
                    wsEnum.Cells["A1"].LoadFromDataTable(dt, true, TableStyles.Medium9);
                    wsEnum.Cells[wsEnum.Dimension.Address].AutoFitColumns();

                    //...and save
                    var fi = new FileInfo(Path.GetTempPath() + Path.GetRandomFileName() + ".xlsx");
                    if (fi.Exists)
                        fi.Delete();

                    pck.SaveAs(fi);

                    Process.Start(fi.FullName);
                }
            }
        }

        private bool CanPrint(object args)
        {
            DataGrid datagrid = args as DataGrid;
            if (datagrid != null)
            {
                if (datagrid.ItemsSource != null)
                    return true;
            }

            return false;
        }

        private IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
        {
            var itemsSource = grid.ItemsSource as IEnumerable;
            if (null == itemsSource) yield return null;
            foreach (var item in itemsSource)
            {
                var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
                if (null != row) yield return row;
            }
        }
        #endregion
    }
}
